package com.geese.module.ceo.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.geese.common.entity.Building;
import com.geese.common.entity.Courier;
import com.geese.common.entity.Order;
import com.geese.common.util.BeanUtil;
import com.geese.common.util.PageUtil;
import com.geese.common.vo.Page;
import com.geese.common.vo.Paginator;
import com.geese.module.business.dto.OrderBusinessDto;
import com.geese.module.business.vo.OrderBusinessQuery;
import com.geese.module.ceo.dao.CourierMapper;
import com.geese.module.ceo.dao.OrderMapper;
import com.geese.module.ceo.dto.OrderDto;
import com.geese.module.ceo.dto.WaiterDto;
import com.geese.module.ceo.vo.OrderCeoQuery;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import ma.glasnost.orika.metadata.Type;

/**
 * Created by yefangyi
 */
@Service
public class OrderService {
	
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CourierMapper courierMapper;
    
	public Paginator<OrderDto> listAllByCeo(OrderCeoQuery query, Integer schoolId, Page page) {
		PageHelper.startPage(page.getCurrPage(), page.getPageSize(), " id DESC");
		List<Order> orderList = orderMapper.listAllByCeo(query, schoolId);
    	Type<Order> soureceType = BeanUtil.getType(Order.class);
		Type<OrderDto> targetType = BeanUtil.getType(OrderDto.class);
		return PageUtil.getPage(orderList, BeanUtil.mapList(orderList, soureceType, targetType));
	}

	public Paginator<OrderBusinessDto> listAllByBusiness(OrderBusinessQuery query, Integer businessId, Page page) {
		PageHelper.startPage(page.getCurrPage(), page.getPageSize(), " id DESC");
		List<Order> orderList = orderMapper.listAllByBusiness(query, businessId);
		List<OrderBusinessDto> result = Lists.newArrayList();
		if(CollectionUtils.isEmpty(orderList)) {
			return PageUtil.getPage(orderList, result);
		}
		Set<Integer> waiterIds = getWaiterSet(orderList);
		Map<Integer, Courier> waiterMap = Maps.newHashMap();
		if(CollectionUtils.isNotEmpty(waiterIds)) {
			List<Courier> waiterList = courierMapper.listAllBySet(getWaiterSet(orderList));
			waiterMap = Maps.uniqueIndex(waiterList, new Function<Courier, Integer>() {
				@Override
				public Integer apply(Courier courier) {
					return courier.getId();
				}
			});
		}
		
		for(Order order : orderList) {
			OrderBusinessDto dto = BeanUtil.map(order, OrderBusinessDto.class);
			if(order.getCourierId() != null) {
				dto.setMobile(waiterMap.get(order.getCourierId()).getMobile());
			}
			result.add(dto);
		}
		return PageUtil.getPage(orderList, result);
	}

	private Set<Integer> getWaiterSet(List<Order> orderList) {
		Set<Integer> resultSet = Sets.newHashSet();
		for(Order order : orderList) {
			resultSet.add(order.getCourierId());
		}
		return resultSet;
	}
}
